﻿using Infra_common;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using xiaodingmarket_Domain.Interfaces;
using xiaodingmarket_Domain.Interfaces.IRepository;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Domain_core.Notificatiions;

namespace xiaodingmarket_service.Controllers
{
    //[Authorize]  //实现验证
    public class PictureController : ApiController
    {
        //1.wwwroot静态资源访问：不支持断点续传
        //2.通过文件流
        public readonly PictureManger _pictureManger;
        public PictureController(PictureManger pictureManger, INotificationHandler<DomainNotification> notifications, IMediatorHandler mediator) : base(notifications, mediator)
        {
            _pictureManger = pictureManger;
        }
        /// <summary>
        /// 发布接口批量上传图片,用户id文件夹-》商品文件夹-》商品原图
        /// 
        /// </summary>
        [HttpPost]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("Picture")]
        public async Task<IActionResult> Post(List<IFormFile> files, string userid, string productid)
        {
            if (!ModelState.IsValid)
            {
                NotifyModelStateErrors();
                return Response();
            }
            List<string> filepath = new List<string>();
            var formCollection = HttpContext.Request.Form.Files;

            if (formCollection!=null)
            {

               filepath = await _pictureManger.AddPicture(formCollection, userid, productid);
            }
            if (filepath != null)
            {
                return Response(filepath);
            }
            else
            {
                NotifyError("IMG", "未成功保存到本地");
                return Response(filepath);
            }

        }
        
       
    }
}
